
package de.fuberlin.enginetesting;

import de.fuberlin.enginetesting.Algorithms.AlgName;
import de.fuberlin.enginetesting.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

//The app to test the engine
public class EngineTesting extends Activity {

	private EditText param1;
	private EditText param2;
	private EditText param3;
	private Engine engine;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_layout2);
		param1 = (EditText) findViewById(R.id.param1Entered);
		param2 = (EditText) findViewById(R.id.param2Entered);
		param3 = (EditText) findViewById(R.id.param3Entered);
		engine = new Engine(this.getApplicationContext());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_engine_testing, menu);
		return true;
	}

	public void onClickedGetMainView(View view) {
		setContentView(R.layout.main_layout2);
		param1 = (EditText) findViewById(R.id.param1Entered);
		param2 = (EditText) findViewById(R.id.param2Entered);
		param3 = (EditText) findViewById(R.id.param3Entered);
	}

	public void onClickedEngineValues(View view) {
		setContentView(R.layout.engine_values);
		((TextView) findViewById(R.id.speedRelationValue)).setText(Double.toString(EngineTesting.round(((double) engine.getRelation()), 7)));
		((TextView) findViewById(R.id.textView4)).setText(Double.toString(EngineTesting.round(((double) engine.getBatteryPerInstruction()), 7)));
		((TextView) findViewById(R.id.textView6)).setText(Double.toString(EngineTesting.round(((double) engine.getBatteryWiFi()), 7)));
		((TextView) findViewById(R.id.textView8)).setText(Double.toString(EngineTesting.round(((double) engine.getBattery3G()), 7)));
		((TextView) findViewById(R.id.textView11)).setText(Long.toString(engine.getPing()));
		if (engine.isConnectionAvailable()) ((TextView) findViewById(R.id.textView14)).setText("Yes");
		else ((TextView) findViewById(R.id.textView14)).setText("No");
		if (engine.isServerAvailable()) ((TextView) findViewById(R.id.textView16)).setText("Yes");
		else ((TextView) findViewById(R.id.textView16)).setText("No");
		((TextView) findViewById(R.id.textView18)).setText(engine.getConnType());
		((TextView) findViewById(R.id.textView20)).setText(engine.getBatteryStatus());
		((TextView) findViewById(R.id.textView23)).setText(Double.toString(EngineTesting.round(engine.getCurrentBattery(), 4)));
	}
	
	private void setConcreteAlgValues (String result) {
		((TextView) findViewById(R.id.textView30)).setText(Double.toString(EngineTesting.round(engine.getAndroidEstimatedRuntime(), 7)));
		((TextView) findViewById(R.id.textView4)).setText(Double.toString(EngineTesting.round(engine.getOffloadingEstimatedTime(), 7)));
		((TextView) findViewById(R.id.textView42)).setText(Double.toString(EngineTesting.round(engine.getServerEstimatedRuntime(), 7)));
		((TextView) findViewById(R.id.textView6)).setText(Double.toString(EngineTesting.round(engine.getAndroidEstimatedNeededEnergy(), 7)));
		((TextView) findViewById(R.id.textView8)).setText(Double.toString(EngineTesting.round(engine.getOffloadingEstimatedNeededEnergy(), 7)));
		if (engine.isOffloadingDone()) {
			((TextView) findViewById(R.id.textView29)).setText("Yes");
			((TextView) findViewById(R.id.textView13)).setText(R.string.ServerEstimatedTime);
		}
		else {
			((TextView) findViewById(R.id.textView29)).setText("No");
			((TextView) findViewById(R.id.textView13)).setText(R.string.AndroidEstimatedTime); 
		}
		((TextView) findViewById(R.id.textView14)).setText(Long.toString(engine.getOverallTime()));
		((TextView) findViewById(R.id.textView16)).setText(engine.getRealServerTime());
		((TextView) findViewById(R.id.textView18)).setText(result);
	}

	public void onClickedDoSomeLoops(View view) {
		String result = engine.execute(AlgName.doSomeLoops, param1.getText().toString());
		setContentView(R.layout.concrete_algorithm_data);
		((TextView) findViewById(R.id.textView0)).setText(R.string.doSomeLoops);
		setConcreteAlgValues(result);
	}

	public void onClickedFibonacciRecursive(View view) {
		String result = engine.execute(AlgName.fibonacciRecursive, param1.getText().toString());
		setContentView(R.layout.concrete_algorithm_data);
		((TextView) findViewById(R.id.textView0)).setText(R.string.FibonacciRecursive);
		setConcreteAlgValues(result);
	}
	
	public void onClickedFibonacciIterative(View view) {
		String result = engine.execute(AlgName.fibonacciIterative, param1.getText().toString());
		if (result.length() >= 19) result = "Overflowed result";
		setContentView(R.layout.concrete_algorithm_data);
		((TextView) findViewById(R.id.textView0)).setText(R.string.FibonacciIterative);
		setConcreteAlgValues(result);
	}
	
	public void onClickedSortArray(View view) {
		String result = engine.execute(AlgName.randomArraySelectionSort, param1.getText().toString());
		setContentView(R.layout.concrete_algorithm_data);
		((TextView) findViewById(R.id.textView0)).setText(R.string.SortArray);
		setConcreteAlgValues(result);
	}

	public void onClickedIsPrime(View view) {
		String result = engine.execute(AlgName.isPrime, param1.getText().toString());
		setContentView(R.layout.concrete_algorithm_data);
		((TextView) findViewById(R.id.textView0)).setText(R.string.isPrime);
		setConcreteAlgValues(result);
	}
	
	private static double round(double nD, int nDec) {
	  return Math.round(nD*Math.pow(10,nDec))/Math.pow(10,nDec);
	}
	
}
